/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-15 17:28:46
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-15 17:28:46
 * @FilePath: /D_C_lib/lw_process/vfork.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cassert>
#include<cstdlib>
#include<unistd.h>
#include<sys/types.h>

int g_val = 10;

int main()
{
    pid_t id = vfork();
    assert(id >=0);
    if(id == 0)
    {
        //child
        g_val = 20;
        std::cout << " I am child :" << getpid() << " : " << "PPID : " << getppid() << std::endl;
        sleep(1);
        std::cout << "我修改了g_val的值从10-->" << g_val << std::endl; 
        exit(0);
    }
    //fataher
    sleep(5);
    std::cout << " I am father :" << getpid() << "g_val:" << g_val << std::endl;
    return 0;
}